package com.skirmisher.inspection.daily.service.impl;

import com.skirmisher.inspection.common.entity.InsLoop;
import com.skirmisher.inspection.common.entity.InsLoopSave;
import com.skirmisher.inspection.common.mapper.InsItemsMapper;
import com.skirmisher.inspection.common.mapper.InsLoopMapper;
import com.skirmisher.inspection.common.service.SystemCodeCacheService;
import com.skirmisher.inspection.daily.entity.*;
import com.skirmisher.inspection.daily.mapper.InsDailyTemplateMapper;
import com.skirmisher.inspection.daily.mapper.InsDaliyTemplateItemsMapper;
import com.skirmisher.inspection.daily.mapper.InsDaliyWorkerMapper;
import com.skirmisher.inspection.daily.service.InsDaliyTemplateService;
import com.skirmisher.inspection.inspection.testController.ResponseModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName insDaliyTemplateServiceImpl
 * @Description TODO
 * @Author hanwangxin
 * @Date 2020/12/11 下午2:08
 * @Version 1.0
 */
@Service
@Slf4j
@Transactional
public class InsDaliyTemplateServiceImpl implements InsDaliyTemplateService {

    @Autowired
    private SystemCodeCacheService systemCodeCacheService;
    @Autowired
    private InsDailyTemplateMapper insDailyTemplateMapper;
    @Autowired
    private InsDaliyTemplateItemsMapper insDaliyTemplateItemsMapper;
    @Autowired
    private InsDaliyWorkerMapper insDaliyWorkerMapper;
    @Autowired
    private InsLoopMapper insLoopMapper;
    @Autowired
    private InsItemsMapper insItemsMapper;

    @Override
    public void saveDailTemp(InsDailyTempSave tempSave, ResponseModel responseModel) {

        //处理主单据数据
        InsDailyTemplate insDailyTemplate = new InsDailyTemplate();
        String tempId = UUID.randomUUID().toString();
        if(!StringUtils.isEmpty(tempSave.getId())){
            tempId = tempSave.getId();
        }
        BeanUtils.copyProperties(tempSave, insDailyTemplate);
        //填写其他参数
        insDailyTemplate.setId(tempId);
        //获取模板编码值
        String nextCode = systemCodeCacheService.getNextCode(SystemCodeCacheService.DAILYTEMP);
        insDailyTemplate.setTempCode(nextCode);
        Date now = new Date();
        insDailyTemplate.setLastUpdateTime(now);
        insDailyTemplate.setLastUpdateWorkerCode(tempSave.getWorkCode());
        insDailyTemplate.setLastUpdateWorkerName(tempSave.getWorkName());
        insDailyTemplate.setWorkerBindType("temp");
        insDailyTemplate.setRecTime(now);
        insDailyTemplate.setTempStatus("0");
        insDailyTemplate.setRecCode(tempSave.getWorkCode());
        insDailyTemplate.setRecName(tempSave.getWorkName());

        insDailyTemplateMapper.insert(insDailyTemplate);
        //处理巡查项目数据
        List<InsDaliyTemplateItems> items = tempSave.getItems();
        if (items != null){
            for (InsDaliyTemplateItems item : items) {
                item.setItemId(UUID.randomUUID().toString());
                item.setTempId(tempId);
                if (StringUtils.isEmpty(item.getItemCode())){
                    String noDateCode = systemCodeCacheService.getNoDateCode(SystemCodeCacheService.ITEMCODE);
                    item.setItemCode(noDateCode);
                }

                insDaliyTemplateItemsMapper.insert(item);
            }
        }


        //处理周期数据
        List<InsLoopSave> loops = tempSave.getLoops();
        for (InsLoopSave loopSave : loops) {
            InsLoop loop = new InsLoop();
            BeanUtils.copyProperties(loopSave, loop);
            loop.setLoopId(UUID.randomUUID().toString());
            loop.setTempId(tempId);
            loop.setLoopName(insDailyTemplate.getTempName());

            insLoopMapper.insert(loop);
        }

        //处理人员数据
        List<InsDaliyWorker> workers = tempSave.getWorkers();
        for (InsDaliyWorker worker : workers) {
            worker.setId(UUID.randomUUID().toString());
            worker.setBindId(tempId);
            worker.setTempId(tempId);
            insDaliyWorkerMapper.insert(worker);
        }

    }

    @Override
    public InsDailyTemplate getTempDetail(String tempId) {
        InsDailyTemplate insDailyTemplate = insDailyTemplateMapper.getById(tempId);
        if (insDailyTemplate == null){
            return null;
        }
        List<InsDaliyTemplateItems> items = insDaliyTemplateItemsMapper.getByTempId(tempId);
        insDailyTemplate.setItems(items);
        List<InsLoop> loops = insLoopMapper.getByTempId(tempId);
        insDailyTemplate.setLoops(loops);
        List<InsDaliyWorker> workers = insDaliyWorkerMapper.getByBindId(tempId);
        insDailyTemplate.setWorkers(workers);
        return insDailyTemplate;

    }

    @Override
    public void getTempPageData(InsDaliyTempQueryEntity query) {

        List<Object> dataList = insDailyTemplateMapper.pageQueryData(query);
        int count = insDailyTemplateMapper.pageQueryCount(query);

        query.setDateList(dataList);
        query.setCount(count);
    }

    @Override
    public void deletDaliyTemp(List<String> tempIds) {


        for (String tempId : tempIds) {
            InsDailyTemplate insDailyTemplate = new InsDailyTemplate();
            insDailyTemplate.setId(tempId);
            insDailyTemplate.setTempStatus("99");
            insDailyTemplateMapper.updateIgnoreNull(insDailyTemplate);
        }
    }

    @Override
    public void updateDaliyTemp(InsDailyTempSave tempSave, ResponseModel responseModel) {

        //删除日常巡查模板 内容 人员 周期
        String tempID = tempSave.getId();
        insDailyTemplateMapper.deleteTemp(tempID);
        insDaliyTemplateItemsMapper.deleteTempItem(tempID);
        insDaliyWorkerMapper.deleteTempWorker(tempID);
        insLoopMapper.deleteTempLoop(tempID);

        saveDailTemp(tempSave,responseModel);
    }

    @Override
    public void startDaliyTemp(List<String> tempIds) {

        for (String tempId : tempIds) {
            InsDailyTemplate insDailyTemplate = new InsDailyTemplate();
            insDailyTemplate.setId(tempId);
            insDailyTemplate.setTempStatus("1");
            insDailyTemplateMapper.updateIgnoreNull(insDailyTemplate);
        }
    }
}
